//
//  TopicPostViewController.m
//  XPhoto
//
//  Created by sentinel on 2020/3/20.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "TopicPostViewController.h"
#import "Post.h"
#import "UIScrollView+x.h"

@interface TopicPostViewController ()

@end

@implementation TopicPostViewController

- (id)init{
    if (self = [super initWithNibName:NSStringFromClass([self class]) bundle:nil]){
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableFooterView = [UIView new];
    self.topic = @"普吉岛";
    [self setTitle:[NSString stringWithFormat:@"#%@", self.topic]];
    [self searchWithOffset:0];
}

- (void)searchWithOffset:(NSInteger)offset{
    PostSearchArgs *args = [PostSearchArgs new];
    args.topic = self.topic;
    args.offset = offset;
    [Post searchByTopicWithArgs:args
                callback:^(id  task, id  photos, NSError * _Nonnull error) {
        if (error) {
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
        }
        if ([photos count]>0) {
            if (offset == 0) {
                self.posts = [NSMutableArray array];
            }
            [self.posts addObjectsFromArray:photos];
            [self.tableView reloadData];
        }
        [self.tableView stopLoading];
        self.tableView.loadMore = [photos count] == args.pageSize;
        
    }];
}

#pragma BKScrollViewDragRefreshDelegate
- (void)scrollViewWillBeginLoadingMore:(BKTableView *)tableView{
    [self searchWithOffset:self.posts.count];
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.tableView scrollViewDidScroll];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.tableView scrollViewDidEndDragging];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
